## Replication commands for: 
## ``Catch-all or Catch and Release? The Electoral Consequences of Social Democratic Parties’ March to the Middle in Western Europe.''
## Johannes Karreth, Jonathan T. Polk and Christopher S. Allen
## Comparative Political Studies 46 (7): pp-pp.
## Last updated: August 24, 2012
## Contact the authors: johannes.karreth@colorado.edu
## Data and replication command stored at:
## http://dvn.iq.harvard.edu/dvn/dv/jkarreth

## Setup

# setwd("") Set your working directory

library(foreign)
library(arm)
library(gtools)
library(MASS)

## Figures appearing in the article

# Fig. 1

partylr <- read.dta("cmp.ches.socdem2.dta")

# Variables:
partylr$label
partylr$ourpid
partylr$ourpname
partylr$year
partylr$cheslr
partylr$cmplr

# Plot Graphs/cmp.ches.lrplot.pdf

y.axis <- length(partylr$label):1 	#create indicator for y.axis, descending so that R orders vars from top to bottom on y-axis
adjust <- .15 						#create object that we will use to adjust points and lines up and down to distinguish between models
    
layout(matrix(c(2,1),1,2), 			#in order to add variable categories and braces to left side of plot, 
    widths = c(1.5, 5))				#we use layout command, create a small second panel on left side.
                					#using c(2,1) in matrix command tells R to create right panel 1st
#layout.show(2) 					#can use this command to check results of layout command (but it must be commented out when creating PDF).

par(mar=c(2,0.5,3,1), lheight = .8)	#set margins for regrEESion plot

par(lheight=.8)

plot(partylr$cheslr, 
	y.axis+adjust, 
	type = "p", 
	axes = F, 
	xlab = "", 
	ylab = "", 
	pch = 19, 
	cex = 0.8, #plot model 1 coefs using black points (pch = 19, default = black), adding the "adjust amount" to the y.axis indicator to move points up
    xlim = c(0,10),  #use na.rm=T since vectors have missing values
    ylim = c(min(y.axis), 
    #xlab = c("Left / Right placement"),
    max(y.axis)), 
    main = "Left/right positions of British, German, and Swedish \nSocial Democratic parties, 1983-2010")

points(partylr$cmplr, y.axis-adjust, pch = 21, cex = 0.8, bg = "white" )

axis(1,at = seq(0,10, by = 2), label = seq(0,10, by = 2), cex.axis=0.8)	#add x-axis and labels; "pretty" creates a sequence of  equally spaced nice values that cover the range of the values in 'x'-- in this case, integers

axis(2, at = y.axis, label = partylr$year, las = 1, tick = T, cex.axis =0.8)#add y-axis and labels; las = 1 makes labels perpendicular to y-axis
#axis(3,pretty(coef.vec.1, 3))		#same as x-axis, but on top axis 

abline(h = y.axis, lty = 2, lwd = .5, col = "light grey")	#draw light dotted line at each variable for dotplot effect

abline(h = 12.5, lty = 1, lwd = .5, col = "black")	## black lines to separate parties
abline(h = 26.5, lty = 1, lwd = .5, col = "black")
#abline(h = 30.5, lty = 1, lwd = .5, col = "black")
#abline(h = 40.5, lty = 1, lwd = .5, col = "black")
#abline(h = 50.5, lty = 1, lwd = .5, col = "black")

# legend(0, 5, c("Chapel Hill Expert Survey", "Comparative Manifesto Project"), pch = c(19,21),bty = "o", cex=0.8)#points(5, 19.5, pch = 19, cex  = 1.2)
#text(-3.7, 19.5, "Model 1", adj = 0,cex  = 1.2)#left-justify text using adj  = 0
#points(-4, 18.5, pch = 21,cex  = 1.2)
#text(-3.7, 18.5, "Model 2", adj = 0,cex  = 1.2)#left-justify text using adj  = 0

# --> Braces made in OmniGraffle

# Fig. 2


voterLR <- read.csv("voters.lrsum.csv")
voterLR.uk <- voterLR[voterLR$ccode==3, ]
voterLR.uk$yearf <- as.factor(voterLR.uk$year)
y.axis <- length(voterLR.uk$yearf):1


pdf("fig2.pdf", 6,5)

plot(voterLR.uk$pleft,
 	voterLR.uk$yearf,
 	type="n",
 	axes=F,
 	xlab="",
 	ylab="Percentage of voters by self-placement",
 	ylim = c(0,60),  
    xlim = c(min(y.axis), max(y.axis)), 
    main = "Left/right positions of British voters, 1973-2010",
    frame=TRUE
    )
    
lines(voterLR.uk$yearf, voterLR.uk$pleft, type="b", lwd=1.5, col="black", pch="L", lty="solid")
lines(voterLR.uk$yearf, voterLR.uk$pcenter, type="b", lwd=1.5, col="gray48", pch="C", lty="solid")
lines(voterLR.uk$yearf, voterLR.uk$pright, type="b", lwd=1.5, col="black", pch="R", lty="dashed")

axis(2,at = seq(0,60, by = 10), label = seq(0,60, by = 10), #cex.axis=0.8
)	

axis(1, at = voterLR.uk$yearf, label = voterLR.uk$year, las = 1, tick = T, #cex.axis =0.8
)

# abline(h = y.axis, lty = 2, lwd = .5, col = "dark grey")	#draw light dotted line 

legend(-0.4, 13, c("Left", "Centrist", "Right"), pch = c("L", "C", "R"), col=c("black", "gray48", "black"), bty = "n", 
 )

dev.off()

# Fig. 3

# 3a


voterLR <- read.csv("voters.lrsum.csv")
voterLR.uk <- voterLR[voterLR$ccode==3, ]
voterLR.uk <- voterLR.uk[voterLR.uk$electionyear==1, ]
voterLR.uk$yearf <- as.factor(voterLR.uk$year)

y.axis <- levels(voterLR.uk$yearf)
y.axis <- length(y.axis):1

pdf("fig3.pdf", 10, 10)

par(mfrow=c(2,2))

plot(voterLR.uk$sdleft, 
	voterLR.uk$yearf, 
	type = "n", 
	axes = F, 
	ylab = "Percentage of Labour voters By self-placement", 
	xlab = "", 
	#cex = 0.8, 
	ylim = c(0,70),  
    xlim = c(min(y.axis), max(y.axis)), 
    main = "",
    frame=TRUE
    )

title(main=list("A: Left/right positions of \nLabour Voters, 1979-2010", cex=1.8))
lines(voterLR.uk$yearf, voterLR.uk$sdleft, type="b", lwd=1.5, col="black", pch="L", lty="solid")
lines(voterLR.uk$yearf, voterLR.uk$sdcenter, type="b", lwd=1.5, col="gray48", pch="C", lty="solid")
lines(voterLR.uk$yearf, voterLR.uk$sdright, type="b", lwd=1.5, col="black", pch="R", lty="dashed")


axis(2,at = seq(0,70, by = 10), label = seq(0,70, by = 10), #cex.axis=0.8
)	

axis(1, at = voterLR.uk$yearf, label = voterLR.uk$year, las = 1, tick = T, #cex.axis =0.8
)

# legend(0.7, 10, c("Left", "Centrist", "Right"), pch = c("L", "C", "R"), col=c("black", "gray48", "black"), bty = "o", cex=0.8
# )

# 3b


voterLR <- read.csv("voters.lrsum.csv")
voterLR.uk <- voterLR[voterLR$ccode==3, ]
voterLR.uk <- voterLR.uk[voterLR.uk$electionyear==1, ]
voterLR.uk$yearf <- as.factor(voterLR.uk$year)

y.axis <- levels(voterLR.uk$yearf)
y.axis <- length(y.axis):1

plot(voterLR.uk$sdstrong, 
	y.axis, 
	type = "n", 
	axes = F, 
	ylab = "Percentage of Labour voters By strength of party ID", 
	xlab = "", 
	ylim = c(0,60),  
    xlim = c(min(y.axis), max(y.axis)), 
    col = "black",
    main = "",
    frame=TRUE
    )

title(main=list("B: Party attachment of \nLabour voters, 1979-2010", cex=1.8))
lines(voterLR.uk$yearf, voterLR.uk$sdstrong, type="b", lwd=1.5, col="black", pch="S", lty="solid")
lines(voterLR.uk$yearf, voterLR.uk$sdmedium, type="b", lwd=1.5, col="black", pch="M", lty="dashed")
lines(voterLR.uk$yearf, voterLR.uk$sdno, type="b", lwd=1.5, col="gray48", pch="W", lty="solid")

axis(2,at = seq(0,60, by = 10), label = seq(0,60, by = 10), #cex.axis=0.8
)	

axis(1, at = voterLR.uk$yearf, label = voterLR.uk$year, las = 1, tick = T, #cex.axis =0.8
)

legend(0.7, 10, c("Strong ID", "Medium ID", "Weak/no ID"), pch = c("S", "M", "W"), col=c("black", "black", "gray48"), bty = "n")

# 3c

EES <- read.dta("EES84-09.SD3.dta")
EES$t_var212 <- as.factor(EES$t_var212)

EES$agedec <- quantcut(EES$age, seq(0,1,by=0.1) )
EES$agedn <- as.numeric(EES$agedec)

EESlab <- EES[EES$t_var113==51320, ]
EESlab1989 <- EES[EESlab$t_ees==1989, ]
EESlab1994 <- EES[EESlab$t_ees==1994, ]
EESlab1999 <- EES[EESlab$t_ees==1999, ]
EESlab2004 <- EES[EESlab$t_ees==2004, ]
EESlab2009 <- EES[EESlab$t_ees==2009, ]

EESspd <- EES[EES$t_var113==41320, ]
EESspd1989 <- EES[EESspd$t_ees==1989, ]
EESspd1994 <- EES[EESspd$t_ees==1994, ]
EESspd1999 <- EES[EESspd$t_ees==1999, ]
EESspd2004 <- EES[EESspd$t_ees==2004, ]
EESspd2009 <- EES[EESspd$t_ees==2009, ]

EESsap <- EES[EES$t_var113==11320, ]
EESsap1989 <- EES[EESsap$t_ees==1989, ]
EESsap1994 <- EES[EESsap$t_ees==1994, ]
EESsap1999 <- EES[EESsap$t_ees==1999, ]
EESsap2004 <- EES[EESsap$t_ees==2004, ]
EESsap2009 <- EES[EESsap$t_ees==2009, ]

# Labour

labm89 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESlab1989, method = c("logistic"))
labm94 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESlab1994, method = c("logistic"))
labm99 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESlab1999, method = c("logistic"))
labm04 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESlab2004, method = c("logistic"))
labm09 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESlab2009, method = c("logistic"))


labm89sum <- summary(labm89)
labm89c <- labm89sum$coefficients[1:7, 1]
labm89se <- labm89sum$coefficients[1:7, 2]

labm94sum <- summary(labm94)
labm94c <- labm94sum$coefficients[1:7, 1]
labm94se <- labm94sum$coefficients[1:7, 2]

labm99sum <- summary(labm99)
labm99c <- labm99sum$coefficients[1:7, 1]
labm99se <- labm99sum$coefficients[1:7, 2]

labm04sum <- summary(labm04)
labm04c <- labm04sum$coefficients[1:7, 1]
labm04se <- labm04sum$coefficients[1:7, 2]

labm09sum <- summary(labm09)
labm09c <- labm09sum$coefficients[1:7, 1]
labm09se <- labm09sum$coefficients[1:7, 2]

coefplot(labm99c, labm99se, xlim=c(-1, 1), main="", varnames=c("Age", "Trade \nunion", "Female", "Upper \nclass", "Urban", "No \ninterest", "Centrist"), mar=c(1,3,7.5,2), cex.var=1, frame=TRUE)
title(main=list("C: Predictors of low party \nattachment for Labour voters", cex=1.8))
coefplot(labm94c, labm94se, xlim=c(-1, 1), add=TRUE, offset=0.15)
coefplot(labm89c, labm99se, xlim=c(-1, 1), add=TRUE, offset=0.3)
coefplot(labm04c, labm04se, xlim=c(-1, 1), add=TRUE, offset=-0.15)
coefplot(labm09c, labm09se, xlim=c(-1, 1), add=TRUE, offset=-0.3)
abline(h = 1.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 2.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 3.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 4.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 5.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 6.5, lty = 2, lwd = .5, col = "gray48")

# 3d

voterLR <- read.csv("voters.lrsum.csv")
voterLR.uk <- voterLR[voterLR$ccode==3, ]
voterLR.uk <- voterLR.uk[voterLR.uk$electionyear==1, ]
voterLR.uk$yearf <- as.factor(voterLR.uk$year)

y.axis <- levels(voterLR.uk$yearf)
y.axis <- length(y.axis):1

plot(voterLR.uk$sdabstain, 
	voterLR.uk$yearf, 
	type = "n", 
	axes = F, 
	ylab = "Percentage of self-identified Labour supporters", 
	xlab = "", 
	ylim = c(0,25),  
    xlim = c(min(y.axis), max(y.axis)), 
    main = "",
    frame=TRUE
    )

title(main=list("D: Abstentions and non-Labour votes \nof Labour supporters, 1979-2010", cex=1.8))
lines(voterLR.uk$yearf, voterLR.uk$sdabstain, type="b", lwd=1.5, col="gray48", pch=15, lty="solid")
lines(voterLR.uk$yearf, voterLR.uk$sdnonSD, type="b", lwd=1.5, col="black", pch=15, lty="solid")


axis(2,at = seq(0,25, by = 5), label = seq(0,25, by = 5), #cex.axis=0.8
)	

axis(1, at = voterLR.uk$yearf, label = voterLR.uk$year, las = 1, tick = T, pos=0.2, lwd=0
)

legend(0.7, 3 , c("Abstained", "Voted for another party"), pch = c(15, 15), col=c("gray48", "black"), bty = "n")
 
dev.off()


# Fig. 4


voterLR <- read.csv("voters.lrsum.csv")
voterLR.germany <- voterLR[voterLR$ccode==1, ]
voterLR.germany$yearf <- as.factor(voterLR.germany$year)
y.axis <- length(voterLR.germany$yearf):1


pdf("fig4.pdf", 6,5)

plot(voterLR.germany$pleft,
 	voterLR.germany$yearf,
 	type="n",
 	axes=F,
 	xlab="",
 	ylab="Percentage of voters by self-placement",
 	ylim = c(0,50),  
    xlim = c(min(y.axis), max(y.axis)), 
    main = "Left/right positions of German voters, 1980-2007",
    frame=TRUE
    )
    
lines(voterLR.germany$yearf, voterLR.germany$pleft, type="b", lwd=1.5, col="black", pch="L", lty="solid")
lines(voterLR.germany$yearf, voterLR.germany$pcenter, type="b", lwd=1.5, col="gray48", pch="C", lty="solid")
lines(voterLR.germany$yearf, voterLR.germany$pright, type="b", lwd=1.5, col="black", pch="R", lty="dashed")

axis(2,at = seq(0,50, by = 10), label = seq(0,50, by = 10), #cex.axis=0.8
)	

axis(1, at = voterLR.germany$yearf, label = voterLR.germany$year, las = 1, tick = T, #cex.axis =0.8
)

# abline(h = y.axis, lty = 2, lwd = .5, col = "dark grey")	#draw light dotted line 

 legend(0, 10, c("Left", "Centrist", "Right"), pch = c("L", "C", "R"), col=c("black", "gray48", "black"), bty = "n", #cex=0.8
 )

dev.off()

# Fig. 5


voterLR <- read.csv("voters.lrsum.csv")
voterLR.germany <- voterLR[voterLR$ccode==1, ]
voterLR.germany <- voterLR.germany[voterLR.germany$electionyear==1, ]
voterLR.germany$yearf <- as.factor(voterLR.germany$year)

y.axis <- levels(voterLR.germany$yearf)
y.axis <- length(y.axis):1

pdf("fig5.pdf", 10, 10)

par(mfrow=c(2,2))

# 5a

plot(voterLR.germany$sdleft, 
	voterLR.germany$yearf, 
	type = "n", 
	axes = F, 
	ylab = "Percentage of SPD voters By self-placement", 
	xlab = "", 
	#cex = 0.8, 
	ylim = c(0,70),  
    xlim = c(min(y.axis), max(y.axis)), 
    main = "",
    frame=TRUE
    )

title(main=list("A: Left/right positions of \nSPD voters, 1983-2006", cex=1.8))
lines(voterLR.germany$yearf, voterLR.germany$sdleft, type="b", lwd=1.5, col="black", pch="L", lty="solid")
lines(voterLR.germany$yearf, voterLR.germany$sdcenter, type="b", lwd=1.5, col="gray48", pch="C", lty="solid")
lines(voterLR.germany$yearf, voterLR.germany$sdright, type="b", lwd=1.5, col="black", pch="R", lty="dashed")


axis(2,at = seq(0,70, by = 10), label = seq(0,70, by = 10), #cex.axis=0.8
)	

axis(1, at = voterLR.germany$yearf, label = voterLR.germany$year, las = 1, tick = T, #cex.axis =0.8
)


voterLR <- read.csv("voters.lrsum.csv")
voterLR.germany <- voterLR[voterLR$ccode==1, ]
voterLR.germany <- voterLR.germany[voterLR.germany$electionyear==1, ]
voterLR.germany$yearf <- as.factor(voterLR.germany$year)

y.axis <- levels(voterLR.germany$yearf)
y.axis <- length(y.axis):1

# 5b

plot(voterLR.germany$sdstrong, 
	y.axis, 
	type = "n", 
	axes = F, 
	ylab = "Percentage of SPD voters By strength of party ID", 
	xlab = "", 
	ylim = c(0,50),  
    xlim = c(min(y.axis), max(y.axis)), 
    col = "black",
    main = "",
    frame=TRUE
    )

title(main=list("B: Party attachment of \nSPD voters, 1983-2006", cex=1.8))
lines(voterLR.germany$yearf, voterLR.germany$sdstrong, type="b", lwd=1.5, col="black", pch="S", lty="solid")
lines(voterLR.germany$yearf, voterLR.germany$sdmedium, type="b", lwd=1.5, col="black", pch="M", lty="dashed")
lines(voterLR.germany$yearf, voterLR.germany$sdno, type="b", lwd=1.5, col="gray48", pch="W", lty="solid")

axis(2,at = seq(0,50, by = 10), label = seq(0,50, by = 10), #cex.axis=0.8
)	

axis(1, at = voterLR.germany$yearf, label = voterLR.germany$year, las = 1, tick = T, #cex.axis =0.8
)

legend(0.7, 11, c("Strong ID", "Medium ID", "Weak/no ID"), pch = c("S", "M", "W"), col=c("black", "black", "gray48"), bty = "n", #cex=0.8
)

# 5c

spdm89 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESspd1989, method = c("logistic"))
spdm94 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESspd1994, method = c("logistic"))
spdm99 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESspd1999, method = c("logistic"))
spdm04 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESspd2004, method = c("logistic"))
spdm09 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESspd2009, method = c("logistic"))


spdm89sum <- summary(spdm89)
spdm89c <- spdm89sum$coefficients[1:7, 1]
spdm89se <- spdm89sum$coefficients[1:7, 2]

spdm94sum <- summary(spdm94)
spdm94c <- spdm94sum$coefficients[1:7, 1]
spdm94se <- spdm94sum$coefficients[1:7, 2]

spdm99sum <- summary(spdm99)
spdm99c <- spdm99sum$coefficients[1:7, 1]
spdm99se <- spdm99sum$coefficients[1:7, 2]

spdm04sum <- summary(spdm04)
spdm04c <- spdm04sum$coefficients[1:7, 1]
spdm04se <- spdm04sum$coefficients[1:7, 2]

spdm09sum <- summary(spdm09)
spdm09c <- spdm09sum$coefficients[1:7, 1]
spdm09se <- spdm09sum$coefficients[1:7, 2]

coefplot(spdm99c, spdm99se, xlim=c(-1, 1), main="", varnames=c("Age", "Trade \nunion", "Female", "Upper \nclass", "Urban", "No \ninterest", "Centrist"), mar=c(1,3,7.5,2), cex.var=1, frame=TRUE)
title(main=list("C: Predictors of low party \nattachment for SPD voters", cex=1.8))
coefplot(spdm94c, spdm94se, xlim=c(-1, 1), add=TRUE, offset=0.15)
coefplot(spdm89c, spdm99se, xlim=c(-1, 1), add=TRUE, offset=0.3)
coefplot(spdm04c, spdm04se, xlim=c(-1, 1), add=TRUE, offset=-0.15)
coefplot(spdm09c, spdm09se, xlim=c(-1, 1), add=TRUE, offset=-0.3)
abline(h = 1.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 2.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 3.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 4.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 5.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 6.5, lty = 2, lwd = .5, col = "gray48")

# 5d


voterLR <- read.csv("voters.lrsum.csv")
voterLR.germany <- voterLR[voterLR$ccode==1, ]
voterLR.germany <- voterLR.germany[voterLR.germany$electionyear==1, ]
voterLR.germany$yearf <- as.factor(voterLR.germany$year)

y.axis <- levels(voterLR.germany$yearf)
y.axis <- length(y.axis):1

plot(voterLR.germany$sdabstain, 
	voterLR.germany$yearf, 
	type = "n", 
	axes = F, 
	ylab = "Percentage of self-identified SPD supporters", 
	xlab = "", 
	ylim = c(0,20),  
    xlim = c(min(y.axis), max(y.axis)), 
    main = "",
    frame=TRUE
    )

title(main=list("D: Abstentions and non-SPD votes \nof SPD supporters, 1983-2006", cex=1.8))
lines(voterLR.germany$yearf, voterLR.germany$sdabstain, type="b", lwd=1.5, col="gray48", pch=15, lty="solid")
lines(voterLR.germany$yearf, voterLR.germany$sdnonSD, type="b", lwd=1.5, col="black", pch=15, lty="solid")


axis(2,at = seq(0,20, by = 5), label = seq(0,20, by = 5), #cex.axis=0.8
)	

axis(1, at = voterLR.germany$yearf, label = voterLR.germany$year, tick = T, pos=0.2, lwd=0
)

legend(0.7, 2.5 , c("Voted for another party", "abstained"), pch = c(15, 15), col=c( "black", "gray48"), bty = "n")

dev.off()

# Fig 6


voterLR <- read.csv("voters.lrsum.csv")
voterLR.sweden <- voterLR[voterLR$ccode==2, ]
voterLR.sweden$yearf <- as.factor(voterLR.sweden$year)
y.axis <- length(voterLR.sweden$yearf):1

pdf("fig6.pdf", 6,5)

plot(voterLR.sweden$pleft,
 	voterLR.sweden$yearf,
 	type="n",
 	axes=F,
 	xlab="",
 	ylab="Percentage of voters by self-placement",
 	ylim = c(0,50),  
    xlim = c(min(y.axis), max(y.axis)), 
    main = "Left/right positions of Swedish voters, 1986-2010",
    frame=TRUE
    )
    
lines(voterLR.sweden$yearf, voterLR.sweden$pleft, type="b", lwd=1.5, col="black", pch="L", lty="solid")
lines(voterLR.sweden$yearf, voterLR.sweden$pcenter, type="b", lwd=1.5, col="gray48", pch="C", lty="solid")
lines(voterLR.sweden$yearf, voterLR.sweden$pright, type="b", lwd=1.5, col="black", pch="R", lty="dashed")

axis(2,at = seq(0,50, by = 10), label = seq(0,50, by = 10), #cex.axis=0.8
)	

axis(1, at = voterLR.sweden$yearf, label = voterLR.sweden$year, las = 1, tick = T, #cex.axis =0.8
)

# abline(h = y.axis, lty = 2, lwd = .5, col = "dark grey")	#draw light dotted line 

legend(0, 10, c("Left", "Centrist", "Right"), pch = c("L", "C", "R"), col=c("black", "gray48", "black"), bty = "n", #cex=0.8
 )

dev.off()

# Fig. 7

# 7a


voterLR <- read.csv("voters.lrsum.csv")
voterLR.sweden <- voterLR[voterLR$ccode==2, ]
voterLR.sweden <- voterLR.sweden[voterLR.sweden$electionyear==1, ]
voterLR.sweden$yearf <- as.factor(voterLR.sweden$year)

y.axis <- levels(voterLR.sweden$yearf)
y.axis <- length(y.axis):1

pdf("fig7.pdf", 10, 10)

par(mfrow=c(2,2))

plot(voterLR.sweden$sdleft, 
	voterLR.sweden$yearf, 
	type = "n", 
	axes = F, 
	ylab = "Percentage of SAP voters By self-placement", 
	xlab = "", 
	#cex = 0.8, 
	ylim = c(0,75),  
    xlim = c(min(y.axis), max(y.axis)), 
    main = "",
    frame=TRUE
    )

title(main=list("A: Left/right positions of \nSAP voters, 1991-2010", cex=1.8))
lines(voterLR.sweden$yearf, voterLR.sweden$sdleft, type="b", lwd=1.5, col="black", pch="L", lty="solid")
lines(voterLR.sweden$yearf, voterLR.sweden$sdcenter, type="b", lwd=1.5, col="gray48", pch="C", lty="solid")
lines(voterLR.sweden$yearf, voterLR.sweden$sdright, type="b", lwd=1.5, col="black", pch="R", lty="dashed")


axis(2,at = seq(0,75, by = 10), label = seq(0,75, by = 10), #cex.axis=0.8
)	

axis(1, at = voterLR.sweden$yearf, label = voterLR.sweden$year, las = 1, tick = T, #cex.axis =0.8
)

# 7b


voterLR <- read.csv("voters.lrsum.csv")
voterLR.sweden <- voterLR[voterLR$ccode==2, ]
voterLR.sweden <- voterLR.sweden[voterLR.sweden$electionyear==1, ]
voterLR.sweden$yearf <- as.factor(voterLR.sweden$year)

y.axis <- levels(voterLR.sweden$yearf)
y.axis <- length(y.axis):1

plot(voterLR.sweden$sdstrong, 
	y.axis, 
	type = "n", 
	axes = F, 
	ylab = "Percentage of SAP voters by strength of party ID", 
	xlab = "", 
	ylim = c(0,50),  
    xlim = c(min(y.axis), max(y.axis)), 
    col = "black",
    main = "",
    frame=TRUE
    )

title(main=list("B: Party attachment of \nSAP voters, 1991-2006", cex=1.8))
lines(voterLR.sweden$yearf, voterLR.sweden$sdstrong, type="b", lwd=1.5, col="black", pch="S", lty="solid")
lines(voterLR.sweden$yearf, voterLR.sweden$sdmedium, type="b", lwd=1.5, col="black", pch="M", lty="dashed")
lines(voterLR.sweden$yearf, voterLR.sweden$sdno, type="b", lwd=1.5, col="gray48", pch="W", lty="solid")

axis(2,at = seq(0,50, by = 10), label = seq(0,50, by = 10), #cex.axis=0.8
)	

axis(1, at = voterLR.sweden$yearf, label = voterLR.sweden$year, las = 1, tick = T, #cex.axis =0.8
)

legend(0.8, 11, c("Strong ID", "Medium ID", "Weak/no ID"), pch = c("S", "M", "W"), col=c("black", "black", "gray48"), bty = "n", #cex=0.8
)

# 7c

sapm99 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESsap1999, method = c("logistic"))
sapm04 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESsap2004, method = c("logistic"))
sapm09 <- polr(t_var212 ~ agedn + tradeunion + female + upperclass + urban + nointerest + lrsd, data=EESsap2009, method = c("logistic"))

sapm99sum <- summary(sapm99)
sapm99c <- sapm99sum$coefficients[1:7, 1]
sapm99se <- sapm99sum$coefficients[1:7, 2]

sapm04sum <- summary(sapm04)
sapm04c <- sapm04sum$coefficients[1:7, 1]
sapm04se <- sapm04sum$coefficients[1:7, 2]

sapm09sum <- summary(sapm09)
sapm09c <- sapm09sum$coefficients[1:7, 1]
sapm09se <- sapm09sum$coefficients[1:7, 2]


coefplot(sapm04c, sapm04se, xlim=c(-1, 1), main="", varnames=c("Age", "Trade \nunion", "Female", "Upper \nclass", "Urban", "No \ninterest", "Centrist"), mar=c(1,3,7.5,2), cex.var=1, frame=TRUE)
title(main=list("C: Predictors of low party \nattachment for SAP voters", cex=1.8))
coefplot(sapm99c, sapm99se, xlim=c(-1, 1), add=TRUE, offset=0.3)
coefplot(sapm09c, sapm09se, xlim=c(-1, 1), add=TRUE, offset=-0.3)
abline(h = 1.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 2.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 3.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 4.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 5.5, lty = 2, lwd = .5, col = "gray48")
abline(h = 6.5, lty = 2, lwd = .5, col = "gray48")

# 7d


voterLR <- read.csv("voters.lrsum.csv")
voterLR.sweden <- voterLR[voterLR$ccode==2, ]
voterLR.sweden <- voterLR.sweden[voterLR.sweden$electionyear==1, ]
voterLR.sweden$yearf <- as.factor(voterLR.sweden$year)

y.axis <- levels(voterLR.sweden$yearf)
y.axis <- length(y.axis):1

plot(voterLR.sweden$sdabstain, 
	voterLR.sweden$yearf, 
	type = "n", 
	axes = F, 
	ylab = "Percentage of self-identified SAP supporters", 
	xlab = "", 
	ylim = c(0,20),  
    xlim = c(min(y.axis), max(y.axis)), 
    main = "",
    frame=TRUE
    )

title(main=list("D: Abstentions and non-SAP votes \nof SAP supporters, 1991-2006", cex=1.8))
lines(voterLR.sweden$yearf, voterLR.sweden$sdabstain, type="b", lwd=1.5, col="gray48", pch=15, lty="solid")
lines(voterLR.sweden$yearf, voterLR.sweden$sdnonSD, type="b", lwd=1.5, col="black", pch=15, lty="solid")


axis(2,at = seq(0,20, by = 5), label = seq(0,20, by = 5), #cex.axis=0.8
)	

axis(1, at = voterLR.sweden$yearf, label = voterLR.sweden$year, las = 1, tick = T, pos=0.2, lwd=0
)

legend(0.8, 21, c("Voted for another party", "Abstained"), pch = c(15, 15), col=c("black", "gray48"), bty = "n")
 
dev.off()